#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  string str;
  cin >> str;
  int n = str.size();
  str = ' ' + str;

  vector<vector<int>> dp(n + 2, vector<int>(n + 1, 0));
  vector<vector<int>> f(n + 2, vector<int>(n + 1, 1));
  for (int l = n; l >= 1; l --) {
    for (int r = l; r <= n; r ++) {
      f[l][r] = 0;
      if (str[l] == str[r]) {
        f[l][r] = f[l + 1][r - 1];
      }
      dp[l][r] = f[l][r] + dp[l + 1][r] + dp[l][r - 1] - dp[l + 1][r - 1];
    }
  }
  int Q;
  cin >> Q;
  while (Q --) {
    int l, r;
    cin >> l >> r;
    cout << dp[l][r] << endl;
  }
}